package com.hzhjhh.view.util;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JComponent;

/**
 * 屏幕工具类
 * 
 * @author 12083
 *
 */
public class ScreenSizeTools {

	/**
	 * 将组件在屏幕居中现实
	 * 
	 * @param component
	 */
	public static void screenCenter(JComponent component) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        Dimension frameSize = component.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        component.setLocation(leftTopPoint(component));
        component.setVisible(false);
	}
	
	/**
	 * 获取组件在屏幕居中显示的左上角顶点坐标
	 * 
	 * @return
	 */
	public static Point leftTopPoint(JComponent component) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        Dimension frameSize = component.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        int x = (screenSize.width - frameSize.width ) / 2;
        int y = (screenSize.height - frameSize.height ) / 2;
        return new Point(x, y);
	}
}
